<?php
class Nutri_Form_Product extends Zend_Form
{
	private $_id;

	public function setId($id = '')
	{
	    $this->_id = $id;    
	}
	
	public function init()
    {
        $this->setMethod('post');

        $this->setDisableLoadDefaultDecorators(true);
 
        $this->addDecorator('FormElements')
             ->addDecorator('Form');
        
        $duplicateValidatorName = new Zend_Validate_Db_NoRecordExists('product', 'name');
        if (!empty($this->_id)) {
        	$duplicateValidatorName->setExclude(array('field' => 'id', 'value' =>  $this->_id));	
        }
        $duplicateValidatorName->setMessage('El nombre del producto \'%value%\' ya existe.');     
             
        $this->addElement('text', 'name', array(
            'label'      => 'Nombre*:',
            'required'   => true,
            'filters'    => array(
        		array('filter'=>'StringTrim'),
        		array('filter'=>'StripTags')
        	),
            'validators' => array(
        		array('validator' => 'StringLength', 'options' => array(3, 255)),
        		array('duplicateFileName' => $duplicateValidatorName)
        	)
        ));
        
        $this->addElement('text', 'description', array(
            'label'      => 'Descripcion:',
            'required'   => false,
            'filters'    => array(
        		array('filter'=>'StringTrim'),
        		array('filter'=>'StripTags')
        	),
            'validators' => array(
        		array('validator' => 'StringLength', 'options' => array(3, 255)))
        ));
		
		$this->addElement('text', 'price', array(
            'label'      => 'Precio*:',
            'required'   => true,
            'filters'    => array(
        		array('filter'=>'StringTrim')
        	),
           'validators' => array(
        		array('validator' => 'StringLength', 'options' => array(0, 9)),
        		array('validator' => 'Float'))
        ));
        
        $this->addElement('submit', 'submit', array(
            'ignore'   => true,
            'label'    => 'guardar',
        	'id' => 'saveProduct'
        ));
        
        //Set the decorators we need:
        $this->setElementDecorators(array(
            'ViewHelper',
            'Label',
            'Errors',
            new Zend_Form_Decorator_HtmlTag(array('tag' => 'div')) //wrap elements in <div>'s
        ));
        
        //Remove label from submit button:
        $this->submit->removeDecorator('Label');
        
    }
}